package team.seekers.hula.record.mind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import team.seekers.hula.common.annotation.ApiResponseBody;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.record.mind.entity.dto.MindAddDTO;
import team.seekers.hula.record.mind.entity.dto.MindUpdateDTO;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.record.mind.entity.dao.MindDO;
import team.seekers.hula.record.mind.entity.dto.MindQueryDTO;
import team.seekers.hula.record.mind.entity.vo.MindListVO;
import team.seekers.hula.record.mind.service.MindService;

import java.util.*;

/**
 * 一个输出，可以是提出一个问题、提出一个观点、写出一个回答、写一个小tip 前端控制器
 * @author hbc
 * @since 2021-02-15
 */
@Controller
@ApiResponseBody
@RequestMapping("/mind")
public class MindController {

    @Autowired
    private MindService mindService;

    @PostMapping("/add")
    public MindListVO add(@RequestBody MindAddDTO mindAddDTO){
        return mindService.addNormal(mindAddDTO.getContent(),mindAddDTO.getVisibility());
    }

    @GetMapping("/delete")
    public void delete(@RequestParam String id){
        mindService.delete(id);
    }


    @PostMapping("/update")
    public void update(@RequestBody MindUpdateDTO updateDTO){
        mindService.updateNormal(updateDTO);
    }


    // 历史页的瀑布流
    @GetMapping("/pageMineOrderByTime")
    public PageResult<MindListVO> pageMineOrderByTime(MindQueryDTO mindQueryDTO){
        mindQueryDTO.setUserId(MindUtils.getUserId());
        return mindService.pageOrderByTime(mindQueryDTO);
    }


    // 关注页的瀑布流
    @GetMapping("/pageFocusOrderByTime")
    public PageResult<MindListVO> pageFocusOrderByTime(MindQueryDTO mindQueryDTO){
        return mindService.pageFocusOrderByTime(mindQueryDTO);
    }

    // 个人页的瀑布流
    @GetMapping("/pageOrderByTime")
    public PageResult<MindListVO> pageOrderByTime(MindQueryDTO mindQueryDTO){
        return mindService.pageOrderByTime(mindQueryDTO);
    }


    // 获取最近的输出状况
    @GetMapping("/recently-mind")
    public ArrayList<Integer> recentlyMind(){
        ArrayList<Integer> mindDeeps = new ArrayList<>();
        // 今天的状况
        Calendar todayCalendar = Calendar.getInstance();
        Date now = new Date(todayCalendar.getTimeInMillis());
        todayCalendar.set(Calendar.SECOND, 0);
        todayCalendar.set(Calendar.MINUTE, 0);
        todayCalendar.set(Calendar.HOUR_OF_DAY, 0);
        todayCalendar.set(Calendar.MILLISECOND, 0);
        Date zero = new Date(todayCalendar.getTimeInMillis());
        int mind = mindService.count(new QueryWrapper<MindDO>().eq("user_id", MindUtils.getUserId()).between("gmt_create", zero, now));
        mindDeeps.add(0,toMindDeep(mind));
        mind = mindService.count(new QueryWrapper<MindDO>().eq("user_id", MindUtils.getUserId()).between("gmt_create", new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*1), zero));
        mindDeeps.add(0,toMindDeep(mind));
        mind = mindService.count(new QueryWrapper<MindDO>().eq("user_id", MindUtils.getUserId()).between("gmt_create", new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*2), new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*1)));
        mindDeeps.add(0,toMindDeep(mind));
        mind = mindService.count(new QueryWrapper<MindDO>().eq("user_id", MindUtils.getUserId()).between("gmt_create", new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*3), new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*2)));
        mindDeeps.add(0,toMindDeep(mind));
        mind = mindService.count(new QueryWrapper<MindDO>().eq("user_id", MindUtils.getUserId()).between("gmt_create", new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*4), new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*3)));
        mindDeeps.add(0,toMindDeep(mind));
        mind = mindService.count(new QueryWrapper<MindDO>().eq("user_id", MindUtils.getUserId()).between("gmt_create", new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*5), new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*4)));
        mindDeeps.add(0,toMindDeep(mind));
        mind = mindService.count(new QueryWrapper<MindDO>().eq("user_id", MindUtils.getUserId()).between("gmt_create", new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*6), new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*5)));
        mindDeeps.add(0,toMindDeep(mind));

        return mindDeeps;
    }

    public Integer toMindDeep(Integer mindNumber){
        if(mindNumber == 0){
            return 0;
        }
        if(mindNumber > 0 && mindNumber <= 5){
            return 1;
        }

        if(mindNumber > 5 && mindNumber <= 10){
            return 2;
        }
        if(mindNumber > 10){
            return 3;
        }
        return 0;
    }

}

